package com.dao;

import com.entity.ClassGrade;
import com.entity.College;
import com.entity.CollegeSort;
import com.entity.Major;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository("collegeDao")
public interface CollegeDao {
    @MapKey(value = "c_name")
    @Select("select * from college")
    Map<String, Map<String, String>> queryAll();

    @MapKey("cName")
    @Select("select * from college")
    @Results({
            @Result(column = "cid", property = "id"),
            @Result(column = "c_name", property = "cName"),
            @Result(
                    column = "cid",
                    property = "majorList",
                    javaType = List.class,
                    many = @Many(select = "com.dao.MajorDao.queryByCollegeId")
            )
    })
    Map<String, College> queryCollegeAll();

    @Select("select * from college where cid=#{cid}")
    @Results({
            @Result(column = "cid", property = "id", id = true),
            @Result(column = "c_name", property = "cName")
    })
    College queryById(Long cid);


    @Select({
            "select g.gid,m.mid,c.cid from class_grade g ",
            "left join major m on g.m_id=m.mid left ",
            "join college c on m.college_id=c.cid"
    })
    @Results({
            @Result(
                    column = "gid",
                    property = "classGrade",
                    javaType = ClassGrade.class,
                    one = @One(select = "com.dao.ClassGradeDao.queryById")
            ),
            @Result(
                    column = "mid",
                    property = "major",
                    javaType = Major.class,
                    one = @One(select = "com.dao.MajorDao.queryById")
            ),
            @Result(
                    column = "cid",
                    property = "college",
                    javaType = College.class,
                    one = @One(select = "com.dao.CollegeDao.queryById")
            )
    })
    @MapKey("college.cName")
    Map<String, CollegeSort> queryCollegeSort();
}
